home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / API.C < prev    next >
C/C++ Source or Header  |  1992-02-04  |  14KB  |  579 lines

  1.  /***************************************************************************
  2.  *
  3.  *     This program contains hllapi functions to interact with the host.
  4.  *
  5.  *     ---------------------------------------------------------------
  6.  *
  7.  *     Ross Abraham    October 2, 1990
  8.  *
  9.  **************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <pcb.h>
  13.  
  14. #define DEBUG      0            /* 1 to insert debugging code */
  15.  
  16. #define MAX_TRIES 18
  17. #define API_File_Name  "API.ERR"
  18. #define X_CLOCK    198
  19. #define QUESTION    24
  20. #define PLUS        53
  21.  
  22. int api_func;                /* function requested */
  23. int api_null = 0;            /* null data string */
  24. int api_len;                /* length of the string */
  25. int api_retc;                /* return code */
  26. char api_nullc;
  27. int irma;
  28. struct query
  29. {
  30.     char far q_short_name;
  31.     char far q_long_name[8];
  32.     char     q_sess_type;
  33.     int     q_ps_size;
  34. } qp[6];
  35.  
  36. char parms[] = "NEWRET NOATTRB NOEAB STRLEN CONLOG ESC=@ AUTORESET SRCHALL SRCHFRWD";
  37.  
  38. extern void CLIM (int function, char *DataString, int DataStringLen,
  39.         int *RetCode, int SoftwareType);
  40.  
  41. extern unsigned int TCLIM(unsigned int *, unsigned char *,
  42.                           unsigned int *, unsigned int *);
  43.  
  44. /*-----------------------------------------------------------------*/
  45. /*--------------------- CONNECT FUNCTION --------------------------*/
  46.  
  47. Connect(char short_name)
  48.  
  49. {
  50.     unsigned int  i;
  51.     unsigned char date[9] = "00/00/00";
  52.     unsigned char far *pointer;
  53.  
  54.     pointer = (char far *)0xF000FFF5;
  55.     for(i=0; i<8; ++i)
  56.        date[i] = *pointer++;
  57.     irma = strcmp(date, "11/08/82") != 0;
  58.     if (irma)
  59.        short_name = 'E';
  60.  
  61.     /*  issue the connect request  */
  62.  
  63.     api_func = PCB_CONNECT;
  64.  
  65.     CLIM( api_func, &short_name, api_len, &api_retc, irma);
  66.  
  67.     switch( api_retc )
  68.     {
  69.     case PCB_RC_CONNECT_OK:
  70.     case PCB_RC_CONNECT_OK_BUSY:
  71.         break;
  72.  
  73.     default:
  74.                 API_Error(PCB_CONNECT, api_retc);
  75.     }
  76.     return(irma);
  77. }
  78.  
  79. /*---------------------------------------------------------*/
  80. /*--------------------QUERY FUNCTION-----------------------*/
  81.  
  82. void Query(char *data_string)
  83.  
  84. {
  85.     /*
  86.      * issue query for session location
  87.      */
  88.  
  89.      int j, i;
  90.     api_func = PCB_QUERY;
  91.     api_len = sizeof(qp);
  92.     CLIM(api_func, ((unsigned char *)&qp[0]), api_len, &api_retc, irma);
  93.     if( api_len == 0 )
  94.     {
  95.                 message(0, "Error 005: No configured sessions");
  96.         exit(1);
  97.     }
  98.  
  99.     for( i = 0, j = 0; i < api_len; ++i )
  100.     {
  101.         if( qp[i].q_sess_type == 'H')
  102.         {
  103.             j = 1;
  104.             break;
  105.         }
  106.     }
  107.     if( j == 0 )
  108.     {
  109.                 message(0, "Error 010: No configured host session");
  110.         exit(1);
  111.     }
  112.  
  113.     i = sizeof(qp);
  114.     for (i = 0; i < api_len; ++i)
  115.     {
  116.         data_string[i] = qp[i].q_short_name;
  117.         strcpy( (data_string + 1 + i * 12), qp[i].q_long_name);
  118.         data_string[i* 12 + 9] = qp[i].q_sess_type;
  119.         data_string[i* 12 + 10] = qp[i].q_ps_size;
  120.     }
  121. }
  122.  
  123. /*-----------------------------------------------------------*/
  124. /*-------------------RESERVE FUNCITON------------------------*/
  125.  
  126. void Reserve()
  127.  
  128. {
  129.     /*
  130.      * reserve the session
  131.      */
  132.     api_func = PCB_RESERVE;
  133.     CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  134.     if (api_retc != PCB_RC_RESERVE_OK)
  135.     {
  136.         api_func = PCB_DISCONNECT;
  137.         CLIM(api_func, &api_nullc, api_null, &api_null, irma);
  138.                 API_Error(PCB_RESERVE, api_retc);
  139.     }
  140. }
  141.  
  142. /*-----------------------------------------------------------*/
  143. /*-------------------SENDKEY FUNCION-------------------------*/
  144.  
  145. void SendKey(char *send_string)
  146. {
  147.         int j;
  148.         char OIA_Str[102];
  149.         int func, len, retc;
  150.  
  151.      api_len = strlen( send_string);
  152.  
  153.     /*
  154.      * send the key string to the emulator
  155.      */
  156.          for (j=0; j<103; ++j) OIA_Str[j] = ' ';
  157.  
  158.          func = PCB_COPYOIA;
  159.          len = 103;
  160.          CLIM(func,OIA_Str,len,&retc,irma);
  161.          //TCLIM(&func, (char *)OIA_Str, &len, &retc);
  162.  
  163.          switch( retc) {
  164.               case PCB_RC_COPYOIA_OK:
  165.               case PCB_RC_COPYOIA_INCOMPL_BUSY:
  166.               case PCB_RC_COPYOIA_INCOMPL_LOCKED:
  167.                    break;
  168.               default:
  169.                    API_Error(func, retc);
  170.                    break;
  171.               }
  172.  
  173.          if (OIA_Str[11] == QUESTION && OIA_Str[12] == PLUS)
  174.               /* Locked key board */
  175.               {
  176.           api_func = PCB_SEND_KEY;
  177.           CLIM(api_func, "@R", 2, &api_retc, irma);
  178.               }
  179.  
  180.          else if( (unsigned)OIA_Str[9] == X_CLOCK)
  181.               Wait();
  182.  
  183.  
  184.     if( api_len != 0 )
  185.     {
  186.         api_func = PCB_SEND_KEY;
  187.         CLIM(api_func, send_string, api_len, &api_retc, irma);
  188.         switch (api_retc) {
  189.            case PCB_RC_SEND_KEY_FAIL_BUSY:
  190.            case PCB_RC_SEND_KEY_FAIL_LOCKED:
  191.             Wait();
  192.             api_len = strlen( send_string);
  193.             api_func = PCB_SEND_KEY;
  194.             CLIM(api_func, send_string, api_len, &api_retc, irma);
  195.             break;
  196.  
  197.            default:
  198.                         break;
  199.          }
  200.  
  201.         if (api_retc != PCB_RC_SEND_KEY_OK )
  202.         {
  203.                    API_Error(api_func, api_retc);
  204.         }
  205.     }
  206. }
  207.  
  208. /*-------------------------------------------------------*/
  209. /*-----------------------WAIT FUNCTION-------------------*/
  210.  
  211. Wait()
  212. {
  213.      int i, j;
  214.      int OK=0;
  215.      char OIA_Str[102];
  216.      int func, len, retc;
  217.  
  218.      /*
  219.      * wait for screen to become unbusy
  220.      */
  221.  
  222.      for(i=0; i<10000 && OK<5; ++i)
  223.          {
  224.  
  225.          for (j=0; j<103; ++j) OIA_Str[j] = ' ';
  226.  
  227.          func = PCB_COPYOIA;
  228.          len = 103;
  229.          CLIM(func,OIA_Str,len,&retc,irma);
  230.          //TCLIM(&func, (char *)OIA_Str, &len, &retc);
  231.  
  232.          switch( retc) {
  233.               case PCB_RC_COPYOIA_OK:
  234.               case PCB_RC_COPYOIA_INCOMPL_BUSY:
  235.               case PCB_RC_COPYOIA_INCOMPL_LOCKED:
  236.                    break;
  237.               default:
  238.                    API_Error(func, retc);
  239.                    break;
  240.               }
  241.  
  242.          if (OIA_Str[11] == QUESTION && OIA_Str[12] == PLUS)
  243.               /* Locked key board */
  244.               SendKey("@R");
  245.  
  246.          else if( (unsigned)OIA_Str[9] == X_CLOCK)
  247.               /* Loop for a while */
  248.               {
  249.               for (j=-32768; j<32767; ++j) j=j;
  250.               }
  251.          else
  252.               {
  253.               OK++;
  254.               break;
  255.               }
  256.          }
  257.  
  258.      api_func = PCB_WAIT;
  259.      CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  260.  
  261.      if( api_retc != PCB_RC_WAIT_OK )
  262.          API_Error(PCB_WAIT, api_retc);
  263.  
  264.      return(1);
  265. }
  266.  
  267. /*
  268. void Wait()
  269. {
  270.     int i;
  271.         api_func = PCB_WAIT;
  272.         for(i=0; i < 5; ++i)
  273.           CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  274.  
  275.         if( api_retc != PCB_RC_WAIT_OK )
  276.                         API_Error(PCB_RESERVE, api_retc);
  277. }
  278. */
  279.  
  280. /*-----------------------------------------------------------*/
  281. /*-------------------SEARCH FUNCTION-------------------------*/
  282.  
  283. void Search(char *search_string, int *location)
  284.  
  285. {
  286.     /*
  287.     * search the presentation space for desired string
  288.     */
  289.     int row, col;
  290.  
  291.     api_len = strlen(search_string);
  292.     if( api_len > 0 )
  293.     {
  294.         api_func = PCB_SEARCH;
  295.  
  296.         api_retc = 1;
  297.         CLIM(api_func, search_string, api_len, &api_retc, irma);
  298.         if (api_retc == 0 )
  299.             *location = 0;
  300.         else
  301.         {
  302.             row = api_retc / 80 + 1;
  303.             col = api_retc - (row - 1) * 80;
  304.             *location = row * 100 + col;
  305.         }
  306.     }
  307.     else
  308.         *location = 0;
  309. }
  310.  
  311. /*-----------------------------------------------------------*/
  312. /*--------------------GET CURSOR POSITION--------------------*/
  313.  
  314. int GetPosition(void)
  315. {
  316.     int    row, col;
  317.     int func, len, retc;
  318.     char nullc;
  319.  
  320.     func = api_func = PCB_GET_POSITION;
  321.     if(irma)
  322.     {
  323.         CLIM(func,&nullc,len,&retc,irma);
  324.        //TCLIM(&func, &nullc, &len, &retc);
  325.        if(retc != PCB_RC_GETPOS_OK)
  326.        {
  327.               API_Error(PCB_GET_POSITION, retc);
  328.        }
  329.        api_retc = len;
  330.     }
  331.     else
  332.       CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  333.  
  334.     row = api_retc / 80 + 1;
  335.     col = api_retc - (row - 1) * 80;
  336.     return(row * 100 + col);
  337. }
  338.  
  339. /*-----------------------------------------------------------*/
  340. /*-------------------SENDSTRING FUNCION-------------------------*/
  341.  
  342. void SendString(char *send_string, int position)
  343. {
  344.     int row, col;
  345.         int j;
  346.         char OIA_Str[102];
  347.         int func, len, retc;
  348.  
  349.     api_len = strlen( send_string);
  350.  
  351.     /*
  352.      * send the string to the emulator
  353.      */
  354.  
  355.          for (j=0; j<103; ++j) OIA_Str[j] = ' ';
  356.  
  357.          func = PCB_COPYOIA;
  358.          len = 103;
  359.          CLIM(func,OIA_Str,len,&retc,irma);
  360.          //TCLIM(&func, (char *)OIA_Str, &len, &retc);
  361.  
  362.          switch( retc) {
  363.               case PCB_RC_COPYOIA_OK:
  364.               case PCB_RC_COPYOIA_INCOMPL_BUSY:
  365.               case PCB_RC_COPYOIA_INCOMPL_LOCKED:
  366.                    break;
  367.               default:
  368.                    API_Error(func, retc);
  369.                    break;
  370.               }
  371.  
  372.          if (OIA_Str[11] == QUESTION && OIA_Str[12] == PLUS)
  373.               /* Locked key board */
  374.               {
  375.           api_func = PCB_SEND_KEY;
  376.           CLIM(api_func, "@R", 2, &api_retc, irma);
  377.               }
  378.  
  379.          else if( (unsigned)OIA_Str[9] == X_CLOCK)
  380.               Wait();
  381.  
  382.     row = position /100;
  383.     col = position - row * 100;
  384.     api_retc = (row - 1) * 80 + col;
  385.  
  386.     api_len = strlen( send_string);
  387.  
  388.     if( api_len != 0 )
  389.     {
  390.         api_func = PCB_COPY_STR_TO_FLD;
  391.         CLIM(api_func, send_string, api_len, &api_retc, irma);
  392.         switch (api_retc) {
  393.            case PCB_RC_COPY_STRTOFLD_FAIL_BUSY:
  394.            case PCB_RC_COPY_STRTOFLD_FAIL_LOCKED:
  395.             Wait();
  396.             api_len = strlen( send_string);
  397.             api_func = PCB_COPY_STR_TO_FLD;
  398.             CLIM(api_func, send_string, api_len, &api_retc, irma);
  399.             break;
  400.            default: break;
  401.          }
  402.  
  403.         if (api_retc != PCB_RC_SEND_KEY_OK && api_retc != PCB_RC_COPY_STRTOFLD_OK_SIZE)
  404.         {
  405.                    API_Error(api_func, api_retc);
  406.         }
  407.     }
  408. }
  409.  
  410. /*--------------------------------------------------------------*/
  411. /*-------------------SET SESSION PARAMETERS FUNCION-------------*/
  412. int SetParameters(char far *send_string)
  413. {
  414.     /*
  415.      * send string
  416.      */
  417.     api_len = sizeof( send_string) - 1;
  418.     if( api_len > 0 )
  419.     {
  420.         api_func = PCB_SET_PARAMS;
  421.         CLIM(api_func, (char *)send_string, api_len, &api_retc, irma);
  422.         if (api_retc != PCB_RC_SET_PARAMS_OK )
  423.         {
  424.                    API_Error(api_func, api_retc);
  425.         }
  426.     }
  427.     return(0);
  428. }
  429.  
  430. /*-----------------------------------------------------------*/
  431. /*---------------RELEASE FUNCTION----------------------------*/
  432. void Release()
  433.  
  434. {
  435.      /* release the session */
  436.     api_func = PCB_RELEASE;
  437.     CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  438.     if (api_retc != PCB_RC_RELEASE_OK)
  439.     {
  440.         api_func = PCB_DISCONNECT;
  441.         CLIM(api_func, &api_nullc, api_null, &api_null, irma);
  442.                 API_Error(PCB_RELEASE, api_retc);
  443.     }
  444. }
  445.  
  446.  
  447. /*------------------------------------------------------------*/
  448. /*-------------------DISCONNECT FUNCTION----------------------*/
  449.  
  450. void Disconnect()
  451.  
  452. {
  453.     /* disconnect the session */
  454.     api_func = PCB_DISCONNECT;
  455.     CLIM(api_func, &api_nullc, api_len, &api_retc, irma);
  456.     if (api_retc != PCB_RC_DISCONNECT_OK)
  457.     {
  458.             API_Error(api_func, api_retc);
  459.     }
  460. }
  461.  
  462. /*--------------------------------------------------------------*/
  463. /*---------------------COPYSTRING FUNCION-----------------------*/
  464.  
  465. void CopyString(char *data_string, int position, int length)
  466.  
  467. {
  468.     int row, col, i;
  469.  
  470.     row = position /100;
  471.     col = position - row * 100;
  472.     api_retc = (row - 1) * 80 + col;
  473.     api_len = length;
  474.     api_func = PCB_COPY_STRING;
  475.  
  476.     CLIM(api_func, data_string, api_len, &api_retc, irma);
  477.     if (api_retc == PCB_RC_COPY_STRING_OK_BUSY)
  478.     {    Wait();
  479.         api_len = length;
  480.         api_retc = (row - 1) * 80 + col;
  481.         api_func = PCB_COPY_STRING;
  482.         CLIM(api_func, data_string, api_len, &api_retc, irma);
  483.     }
  484.  
  485.     if (api_retc != PCB_RC_COPY_STRING_OK)
  486.     {
  487.            API_Error(api_func, api_retc);
  488.     }
  489.  
  490.     /* since strings are null terminated in C, remove nulls copied */
  491.     /* from the screen */
  492.     for (i = 0; i <= length - 1; ++i)
  493.         if (data_string[i] == '\0')
  494.             data_string[i] = ' ';
  495.  
  496.     data_string[length] = '\0';
  497. }
  498.  
  499.  
  500. /*------------------------------------------------------------*/
  501. /*----------------COPY PRESENTATION SPACE FUNCION-------------*/
  502.  
  503. void CopyPS(char *data_string)
  504.  
  505. {
  506.     int i;
  507.  
  508.     api_func = PCB_COPYPS;
  509.      api_len = 1920;
  510.     CLIM(api_func, data_string, api_len, &api_retc, irma);
  511.  
  512.     if (api_retc == PCB_RC_COPYPS_OK_BUSY)
  513.     {    Wait();
  514.         api_func = PCB_COPYPS;
  515.         CLIM(api_func, data_string, api_len, &api_retc, irma);
  516.     }
  517.  
  518.     if (api_retc != PCB_RC_COPYPS_OK)
  519.     {
  520.                    API_Error(api_func, api_retc);
  521.     }
  522.  
  523.     /*  since strings are null terminated in C, nulls from the screen */
  524.     /*  must be removed. */
  525.     for (i = 0; i<= 1919; ++i)
  526.         if (data_string[i] == '\0')
  527.             data_string[i] = ' ';
  528.  
  529.        /* the following statement forces the string to be 1920 chars*/
  530.     data_string[1920] = '\0';
  531. }
  532.  
  533. /*------------------------------------------------------------*/
  534. /*----------------COPY OPERATION INFORMATION AREA FUNCION-----*/
  535.  
  536. void CopyOIA(char *data_string)
  537.  
  538. {
  539.     int i;
  540.     char d_string[103];
  541.     int retc;
  542.     int func;
  543.     int len;
  544.  
  545.     for (i=0; i<103; ++i) d_string[i] = 0;
  546.  
  547.     func = PCB_COPYOIA;
  548.      CLIM(func,d_string,len,&retc,irma);
  549.     //TCLIM(&func, d_string, &len, &retc);
  550.  
  551.     if (api_retc != PCB_RC_COPYOIA_OK && api_retc != PCB_RC_COPYOIA_INCOMPL_BUSY && api_retc != PCB_RC_COPYOIA_INCOMPL_LOCKED)
  552.     {
  553.                    API_Error(api_func, api_retc);
  554.     }
  555.  
  556.     for (i=0; i<103; ++i)
  557.        data_string[i] = d_string[i];
  558.  
  559.     data_string[103] = 0;
  560. }
  561.  
  562.  
  563. /*------------------------------------------------------------*/
  564. /*----------------------DEBUGGER CODE MAIN--------------------*/
  565.  
  566. #if DEBUG
  567.     void main(void)
  568.     {
  569.     char string[150];
  570.  
  571.     Connect('C');
  572.  
  573.         SendKey("@y");
  574.         SendString("ALPH", 102);
  575.     Wait();
  576.  
  577.     }
  578. #endif
  579.